import axios from 'axios';
import { Message } from 'element-ui';

const instance = axios.create({
  baseURL: 'http://192.168.137.35:8080',
  timeout: 20000,
});

instance.interceptors.request.use((config) => {
  // eslint-disable-next-line
  config.headers.authorization = localStorage.getItem('__token__');
  return config;
});

instance.interceptors.response.use((res) => {
  if (res.status < 200 || res.status > 300) {
    return Promise.reject(res.statusText);
  }

  if (res.data.err_message) {
    Message.warning(res.data.err_message);
    return Promise.reject(res.data.err_message);
  }

  return res.data.data;
});

export default instance;
